﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

public class InitXML
{
    private static InitXML self;

    private List<string> InitCompleteXml = new List<string>();

    public InitXML() { }

    public static InitXML getInstance()
    {
        if (self == null)
        {
            self = new InitXML();
        }
        return self;
    }

    public void AnalyzeXml(string xmlName, ObjectData sceneObj)
    {
        string path = ResPath.ConfigFile + xmlName + ".xml";

        if (InitCompleteXml.IndexOf(path) == -1)
        {
            InitCompleteXml.Add(path);

            XmlReader reader = null;
            try
            {
                // 创建XmlReaderSettings对象
                XmlReaderSettings settings = new XmlReaderSettings();
                // 设置XmlReaderSettings对象的属性
                settings.ProhibitDtd = false;
                // 使用XmlReaderSettings对象来创建XmlReader对象
                reader = XmlReader.Create(path, settings);

                ReadXml(reader, sceneObj, xmlName);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
        }
    }

    private void ReadXml(XmlReader reader, ObjectData sceneObj, string xmlName)
    {
        try
        {
            int propertyIndex = 0;
            int recordIndex = 0;
            Record record = new Record("0");
            List<int> arrType = new List<int>();
            // 解析文件，并显示每一个节点
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        if (reader.Name == "field")
                        {
                            if (reader.HasAttributes)   // 属性
                            {
                                string key="";
                                string value="";
                                int type=1;
                                while (reader.MoveToNextAttribute())
                                {
                                    if (reader.Name=="ID")//唯一属性
                                    {
                                        key = reader.Value;
                                    }
                                    if (reader.Name=="Value")//属性值
                                    {
                                        value = reader.Value;
                                    }
                                    if (reader.Name == "type")//属性类型
                                    {
                                        type = int.Parse(reader.Value);
                                    }
                                }
                                sceneObj.HandleProperties(xmlName + "_" + key, value);
                                sceneObj.ProertyNames.HandleProperties(xmlName + "_" + propertyIndex, xmlName + "_" + key);
                                sceneObj.ProertyTypes.HandleProperties(xmlName + "_" + propertyIndex, type);
                                propertyIndex++;
                            }
                        }
                        else if (reader.Name == "record")
                        {
                            if (arrType.Count!=0)
                            {
                                List<int> typeArr = new List<int>(arrType);
                                record.SetColType(typeArr);
                                sceneObj.HandlePropertiesEx(xmlName + "_" + (recordIndex - 1),record);
                                arrType.Clear();
                            }
                            record = new Record(xmlName+"_"+recordIndex);
                            recordIndex++;
                           break;
                        }
                        else if (reader.Name == "column")
                        {
                            if (reader.HasAttributes)   // 属性
                            {
                                int type = 1;
                                while (reader.MoveToNextAttribute())
                                {
                                    if (reader.Name == "type")
                                    {
                                        type = int.Parse(reader.Value);
                                        arrType.Add(type);
                                    }
                                }
                            }
                            break;
                        }




                        break;
                }
            }
        }
        catch (XmlException ex)
        {
            Console.WriteLine(ex.Message);
        }
    }



}